<#-- 注册页 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<base href="${baseURL}">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>注册 - ${title}</title>
	<meta name="keywords" content="${keywords}"/>
	<meta name="description" content="${description}"/>
	<link rel="shortcut icon" type="image/x-icon" href="${baseURL}${commonPath}images/favicon.ico" media="screen" />
	<link href="${commonPath}css/common.css" type="text/css" rel="stylesheet">
	<script src="${commonPath}js/cryptoJS/core.js" language="javascript" type="text/javascript"></script>
	<script src="${commonPath}js/cryptoJS/sha256.js" language="javascript" type="text/javascript"></script>
		
	<script language="javascript" src="${commonPath}js/tool.js" type="text/javascript"></script>
	<script language="javascript" src="${commonPath}js/ajax.js" type="text/javascript"></script>
	<script language="javascript" src="${commonPath}js/json2.js" type="text/javascript"></script>
</head>
<script type="text/javascript" language="javascript"> 
	//确认提交
	function sureSubmit(objForm){
		if(allVerification() == false){
			alert("请填好资料再提交");
			return;
		}

		//密码需SHA256加密
		var password = document.getElementById("password").value;
		if(password != ""){
			document.getElementById("password").value = CryptoJS.SHA256(password);
		}
		//密码提示答案需SHA256加密
		var answer = document.getElementById("answer").value;
		if(answer != ""){
			document.getElementById("answer").value = CryptoJS.SHA256(answer);
		}
		

		objForm.submit();
	}
	
	//ajax提交
	function ajaxSubmit(){
	
		if(allVerification() == false){
			alert("请填好资料再提交");
			return;
		}
	
		var parameter = getParameter();
		if(parameter.indexOf("&") == 0){
			parameter = parameter.substring(1,parameter.length);
		}
		post_request(function(value){
			if(value != ""){
				var returnValue = JSON.parse(value);//JSON转为对象
				
				var value_success = "";
				var value_error = null;
				var value_jumpUrl = "";
				var value_captchaKey = null;
				for (var key in returnValue) {
					if (key == "success") {
						value_success = returnValue[key];
					} else if (key == "error") {
						value_error = returnValue[key];
					}else if (key == "jumpUrl") {
						value_jumpUrl = returnValue[key];
					}else if (key == "captchaKey") {
						value_captchaKey = returnValue[key];
					}
				}
				if(value_success == "true"){//成功
					window.location.href = (getBasePath()+value_jumpUrl);
				
				}else{//失败
					for(var error in value_error){
						if(error != ""){
							if(document.getElementById(error+"_error") != null){
								document.getElementById(error+"_error").innerHTML=value_error[error];	
							}
						}
					}
					
					if (value_captchaKey != null) {
						
						//设置验证码Key
						document.getElementById("captchaKey").value = value_captchaKey;
						//设置验证码图片
						replaceCaptcha();
						
					}
					
				}
			}	
		},
			"register?&timestamp=" + new Date().getTime(), true,parameter);

	}
	
	//组装参数
	function getParameter(){
		var parameter = "";

		//url跳转参数
		var jumpUrl = getUrlParam("jumpUrl");
		if(jumpUrl != null){
			parameter += "&jumpUrl="+jumpUrl;
		}
		//令牌标记
		var token = document.getElementById("token").value;
		parameter += "&token="+token;
		
		//验证码Key
		var captchaKey = document.getElementById("captchaKey");
		if(captchaKey != null){
			parameter += "&captchaKey="+encodeURIComponent(captchaKey.value);
		}
		//验证码值
		var captchaValue = document.getElementById("captchaValue");
		if(captchaValue != null){
			parameter += "&captchaValue="+encodeURIComponent(captchaValue.value);
		}

		//用户名
		var userName = document.getElementById("userName").value;
		parameter += "&userName="+encodeURIComponent(userName);
		//密码需SHA256加密
		var password = document.getElementById("password").value;
		if(password != ""){
			parameter += "&password="+ CryptoJS.SHA256(password);
		}
		
		//密码提示问题
		var issue = document.getElementById("issue").value;
		parameter += "&issue="+encodeURIComponent(issue);
		//密码提示答案需SHA256加密
		var answer = document.getElementById("answer").value;
		if(answer != ""){
			parameter += "&answer=" + CryptoJS.SHA256(answer);
		}
		
		//邮箱
		var email = document.getElementById("email").value;
		parameter += "&email=" +encodeURIComponent(email);
		
		//自定义表单name集合
		var array = new Array();
		
		//读取表单所有的input标签
		var all_input = document.getElementsByTagName("input");
		for(var i=0;i<all_input.length; i++){
			var obj = all_input[i];
			if(obj.name.substring(0,11) =="userCustom_"){
				var isExist = false;//是否已存在数组中
				//如果存在，则不添加
				for(var j=0;j<array.length; j++){
					if(array[j] == all_input[i].name){
						isExist = true;
						break;
					}
				}
				if(isExist == false){
					array.push (all_input[i].name); 
				}		
			}
		}
		//读取表单所有的select标签
		var all_select = document.getElementsByTagName("select");
		for(var i=0;i<all_select.length; i++){
			var obj = all_select[i];
			if(obj.name.substring(0,11) =="userCustom_"){
				var isExist = false;//是否已存在数组中
				//如果存在，则不添加
				for(var j=0;j<array.length; j++){
					if(array[j] == all_select[i].name){
						isExist = true;
						break;
					}
				}
				if(isExist == false){
					array.push (all_select[i].name); 
				}		
			}
		}
		//读取表单所有的textarea标签
		var all_textarea = document.getElementsByTagName("textarea");
		for(var i=0;i<all_textarea.length; i++){
			var obj = all_textarea[i];
			if(obj.name.substring(0,11) =="userCustom_"){
				var isExist = false;//是否已存在数组中
				//如果存在，则不添加
				for(var j=0;j<array.length; j++){
					if(array[j] == all_textarea[i].name){
						isExist = true;
						break;
					}
				}
				if(isExist == false){
					array.push (all_textarea[i].name); 
				}		
			}
		}

		for(var i=0;i<array.length; i++){
			//读取表单参数
			var obj = document.getElementsByName(array[i]);
			if(obj[0].type == "text"){//输入框
				parameter += "&"+obj[0].name+"="+encodeURIComponent(obj[0].value);
			}
			
			if(obj[0].type == "radio"){//单选
				for(var j=0;j<obj.length;j++){ 
					if(obj[j].checked){
						parameter += "&"+obj[0].name+"="+obj[j].value;
						break; 
					}
				}
			}
			if(obj[0].type == "checkbox"){//多选
				for(var j=0;j<obj.length;j++){ 
					if(obj[j].checked){
						parameter += "&"+obj[0].name+"="+obj[j].value;
					}
				}
			}
			if(obj[0].type.substring(0,6) == "select"){//下拉列表
				for(var j=0;j<obj[0].length;j++){ 
					if(obj[0][j].selected){
						parameter += "&"+obj[0].name+"="+obj[0][j].value;
					}
				}
			}
			if(obj[0].type == "textarea"){//表单域
				parameter += "&"+obj[0].name+"="+encodeURIComponent(obj[0].value);
			
			}
		}

		//删除第一个&号,防止因为多了&号而出现警告: Parameters: Invalid chunk ignored.信息
		if(parameter.indexOf("&") == 0){
			parameter = parameter.substring(1,parameter.length);
		}
		return parameter;
	}
	
	
	
	
	//验证参数
	function verification(field){
		if(field == "userName"){//用户名
			
			var parameter = document.getElementById(field).value;
			if(parameter != ""){
			
				var parameter_trim = trim(parameter);
				if(parameter_trim != ""){
					if(getStringLeng(parameter_trim) < 3){
						document.getElementById(field+"_error").innerHTML = "用户名长度不能小于3位";
						return false;
					}
					if(getStringLeng(parameter_trim) >= 20){
						document.getElementById(field+"_error").innerHTML = "用户名长度不能大于20位";
						return false;
					}
					var pattern =  /^\w+$/;
	            	if(!pattern.test(parameter_trim)) { 
	                	document.getElementById(field+"_error").innerHTML = "用户名只能输入由数字、26个英文字母或者下划线组成";
						return false;
	            	}
	            	
	            	get_request(function(value){
	            		if(value == "true"){
		            		document.getElementById(field+"_error").innerHTML = "该用户名已存在";
	            		}
	            	},
			 			"userVerification.htm?userName="+parameter_trim+"&timestamp=" + new Date().getTime(), true);
	            	
	            	
	            	
					
				}else{
					document.getElementById(field+"_error").innerHTML = "用户名不能为空";
					return false;
				}
			}else{
			
				document.getElementById(field+"_error").innerHTML = "用户名不能为空";
				return false;
			}
		}
		if(field == "password"){//密码
			var parameter = document.getElementById(field).value;
			if(parameter != ""){
				var parameter_trim = trim(parameter);
				if(parameter_trim != ""){
					if(getStringLeng(parameter_trim) < 6){
						document.getElementById(field+"_error").innerHTML = "密码长度不能小于6位";
						return false;
					}
					if(getStringLeng(parameter_trim) > 20){
						document.getElementById(field+"_error").innerHTML = "密码长度不能大于20位";
						return false;
					}
				}else{
					document.getElementById(field+"_error").innerHTML = "密码不能为空";
					return false;
				}
			}else{
				document.getElementById(field+"_error").innerHTML = "密码不能为空";
				return false;
			}
		}
		if(field == "confirmPassword"){//确认密码
			var password = document.getElementById("password").value;
			
			var parameter = document.getElementById(field).value;
			if(password != parameter){
				document.getElementById(field+"_error").innerHTML = "两次密码不相等";
				return false;
			}
		}
		if(field == "issue"){//密码提示问题
			var parameter = document.getElementById(field).value;
			if(parameter != ""){
				var parameter_trim = trim(parameter);
				if(parameter_trim != ""){
					if(getStringLeng(parameter_trim) < 6){
						document.getElementById(field+"_error").innerHTML = "提示问题长度不能小于6位";
						return false;
					}
					if(getStringLeng(parameter_trim) > 30){
						document.getElementById(field+"_error").innerHTML = "提示问题长度不能大于30位";
						return false;
					}
				}else{
					document.getElementById(field+"_error").innerHTML = "提示问题不能为空";
					return false;
				}
			}else{
				document.getElementById(field+"_error").innerHTML = "提示问题不能为空";
				return false;
			}
		}
		if(field == "answer"){//密码提示答案
			var parameter = document.getElementById(field).value;
			if(parameter != ""){
				var parameter_trim = trim(parameter);
				if(parameter_trim != ""){
					if(getStringLeng(parameter_trim) < 6){
						document.getElementById(field+"_error").innerHTML = "提示答案长度不能小于6位";
						return false;
					}
					if(getStringLeng(parameter_trim) > 30){
						document.getElementById(field+"_error").innerHTML = "提示答案长度不能大于30位";
						return false;
					}
				}else{
					document.getElementById(field+"_error").innerHTML = "提示答案不能为空";
					return false;
				}
			}else{
				document.getElementById(field+"_error").innerHTML = "提示答案不能为空";
				return false;
			}
		}
		
		if(field == "email"){//邮箱
			var parameter = document.getElementById(field).value;
			if(parameter != ""){
				var parameter_trim = trim(parameter);
				if(parameter_trim != ""){
					if(getStringLeng(parameter_trim) > 60){
						document.getElementById(field+"_error").innerHTML = "邮箱地址不能超过60个字符";
						return false;
					}
				
					var pattern =  /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	            	if(!pattern.test(parameter_trim)) { 
	                	document.getElementById(field+"_error").innerHTML = "邮箱格式错误";
						return false;
	            	}
				}
				
			}
		}
		
		if(field == "captchaValue"){//验证码
			if( document.getElementById("captchaKey") != null){
				var captchaKey = document.getElementById("captchaKey").value;
				var parameter = document.getElementById(field).value;
				if(parameter != ""){
				
					var parameter_trim = trim(parameter);
					if(parameter_trim != ""){
						get_request(function(value){
			            	if(value == "false"){
				            	document.getElementById(field+"_error").innerHTML = "验证码错误";
			            	}
			            },
					 		"userVerification.htm?captchaKey="+captchaKey+"&captchaValue="+parameter_trim+"&timestamp=" + new Date().getTime(), true);
			            	
					}else{
						document.getElementById(field+"_error").innerHTML = "验证码不能为空";
						return false;
					}
				}else{
					document.getElementById(field+"_error").innerHTML = "验证码不能为空";
					return false;
				}
			}
		}
		        	
		if(document.getElementById(field+"_error") != null){
			document.getElementById(field+"_error").innerHTML = "";
		}
		return true;
	}
	
	//验证全部参数
	function allVerification(){
		var isVerification = true;
		if(verification("userName") == false){
			isVerification = false;
		}
		if(verification("password") == false){
			isVerification = false;
		}
		if(verification("confirmPassword") == false){
			isVerification = false;
		}
		if(verification("issue") == false){
			isVerification = false;
		}
		if(verification("answer") == false){
			isVerification = false;
		}
		if(verification("captchaValue") == false){
			isVerification = false;
		}
		return isVerification;
	}
	
	//更换验证码
	function replaceCaptcha(){
		var captchaKey = document.getElementById("captchaKey").value;
		document.getElementById("captcha").src = "captcha/"+captchaKey+".jpg?" + Math.random(); 
	}
	

	//禁止浏览器后退
	window.history.forward(1);
	
</script>


<body >
<#-- 引入页头 -->
<@include action="${newPublic_2}"/>
<div class="fixedBottom wrapper clearfix">
<form method="post" action="register?jumpUrl=${jumpUrl}" class="pure-form">
	<!-- 令牌标记 -->
	<input type="hidden" id="token" name="token" value="${token}">


	<div class="registerModule wrapper">
		<h2>会员注册</h2>
	
		<div class="item">
			<table>
				<tr>
					<th><span class="required">*</span><label>用户名：</label></th>
					<td><input type="text" id="userName" name="userName" class="inputBox formText" autocomplete="off" size="25" maxlength="20" value="<#if user??>${user.userName}</#if>" onBlur="verification(this.id)"/></td>
					<td>
						<span id="userName_error" class="error">${(error['userName'])!}</span> 
					</td>
				</tr>
			</table>
		</div>
		<div class="item">
			<table>
				<tr>
					<th><span class="required">*</span><label>密码：</label></th>
					<td><input type="password" id="password" name="password" class="inputBox formText" autocomplete="off" size="25" maxlength="20" onBlur="verification(this.id)"/></td>
					<td>
						<span id="password_error" class="error">${(error['password'])!}</span>  
					</td>
				</tr>
			</table>
		</div>
		<div class="item">
			<table>
				<tr>
					<th><span class="required">*</span><label>确认密码：</label></th>
					<td><input type="password" id="confirmPassword" class="inputBox formText" autocomplete="off" size="25" maxlength="20" onBlur="verification(this.id)"/></td>
					<td>
						<span id="confirmPassword_error" class="error"></span>  
					</td>
				</tr>
			</table>
		</div>
		<div class="item">
			<table>
				<tr>
					<th><span class="required">*</span><label>密码提示问题：</label></th>
					<td><input type="text" id="issue" name="issue" class="inputBox formText" autocomplete="off" size="25" maxlength="20" value="<#if user??>${user.issue}</#if>" onBlur="verification(this.id)"/></td>
					<td>
						<span id="issue_error" class="error">${(error['issue'])!}</span> 
					</td>
				</tr>
			</table>
		</div>
		<div class="item">
			<table>
				<tr>
					<th><span class="required">*</span><label>密码提示答案：</label></th>
					<td><input type="text" id="answer" name="answer" class="inputBox formText" autocomplete="off" size="25" maxlength="20" onBlur="verification(this.id)"/></td>
					<td>
						<span id="answer_error" class="error">${(error['answer'])!}</span> 
					</td>
				</tr>
			</table>
		</div>
		<div class="item">
			<table>
				<tr>
					<th><label>邮箱：</label></th>
					<td><input type="text" id="email" name="email" class="inputBox formText" autocomplete="off" size="25" maxlength="20" value="<#if user??>${user.email}</#if>" onBlur="verification(this.id)"/></td>
					<td>
						<span id="email_error" class="error">${(error['email'])!}</span> 
					</td>
				</tr>
			</table>
		</div>
		
		<!-- 用户自定义注册功能项 -->
		<#list userCustomList as userCustom>
		<div class="item">
			<table>
				<tr>
					<th><#if userCustom.required == true><span class="required">*</span></#if><label>${userCustom.name}：</label></th>
					<td>
						<#if userCustom.chooseType ==1>
							<input type="text" class="inputBox formText" name="userCustom_${userCustom.id}" value="<#list userCustom.userInputValueList as userInputValue>${userInputValue.content}</#list>" size="${userCustom.size}" maxlength="${userCustom.maxlength}">
						</#if>
						<#if userCustom.chooseType ==2>
							<#if userCustom.itemValue?has_content>
								<#list userCustom.itemValue?keys as itemValue>
								<#-- 选中项 -->
								<#assign _checked = "">
								<#list userCustom.userInputValueList as userInputValue>
									<#if userInputValue.options == itemValue>
										<#assign _checked=" checked='checked'">
									</#if>
								</#list>
								
								<#-- 默认选第一项 -->
								<#if itemValue_index == 0  && userCustom.userInputValueList?exists && userCustom.userInputValueList?size == 0> 
								<#assign _checked = " checked='checked'">
								</#if>
								<input type="radio" name="userCustom_${userCustom.id}" value="${itemValue}" ${_checked} >${userCustom.itemValue[itemValue]}
								</#list>
							</#if>
						</#if>
						<#if userCustom.chooseType ==3>
							<#if userCustom.itemValue?has_content>
								<#list userCustom.itemValue?keys as itemValue>
									<#-- 选中项 -->
									<#assign _checked = "">
									<#list userCustom.userInputValueList as userInputValue>
										<#if userInputValue.options == itemValue>
											<#assign _checked=" checked='checked'">
										</#if>
									</#list>
									<input type="checkbox" name="userCustom_${userCustom.id}" value="${itemValue}" ${_checked}>${userCustom.itemValue[itemValue]}
								</#list>
							</#if>
						</#if>
						<#if userCustom.chooseType ==4>
							<select name="userCustom_${userCustom.id}" <#if userCustom.multiple == true> multiple='multiple'</#if> <#if userCustom.selete_size != null> size='${userCustom.selete_size}'</#if>>
								<#if userCustom.itemValue?has_content>
								<#list userCustom.itemValue?keys as itemValue>
									<#assign _selected = "">
									<#list userCustom.userInputValueList as userInputValue>
										<#if userInputValue.options == itemValue>
											<#assign _selected=" selected='selected'">
										</#if>
									</#list>			
									<option value="${itemValue}" ${_selected}>${userCustom.itemValue[itemValue]}</option>		
								</#list>
								</#if>	
							</select>
						</#if>
						<#if userCustom.chooseType ==5>
							<textarea name="userCustom_${userCustom.id}" rows="${userCustom.rows}" cols="${userCustom.cols}"><#list userCustom.userInputValueList as userInputValue>${userInputValue.content}</#list></textarea>
						</#if>
					</td>
					<td>
						<span class="help">${userCustom.tip}</span>
						<#assign userCustom_id = "userCustom_"+userCustom.id>
						<span id="userCustom_${userCustom.id}_error" class="error">${error[userCustom_id]}</span>
					</td>
				</tr>
			</table>
		</div>
		</#list>

		<#-- 验证码 -->
		<#if captchaKey != ''>
			<div class="item">
				<table>
					<tr>
						<th><label><span class="required">*</span>验证码：</label></th>
						<td >
							<!-- 验证码Key -->
							<input type="hidden" id="captchaKey" name="captchaKey" value="${captchaKey}">
							<input id="captchaValue" name="captchaValue" type="text" class="inputBox_captcha formText" size="7" maxlength="7" autocomplete="off" onBlur="verification(this.id)"/>
						</td>
						<td>
							<img id="captcha" src="captcha/${captchaKey!}.jpg" onClick="replaceCaptcha()"/>
							<span id="captchaValue_error" class="error">${(error['captchaValue'])!}</span> 
						</td>
					</tr>
				</table>
			</div>
		</#if>
		<div class="item">
			<div class="registerSubmit">
				<span class="submitButton"><INPUT type="button" value="提交" onClick="javascript:sureSubmit(this.form);"></span>
				<span id="register_error" class="error">${(error['register'])!}</span> 
				<!-- 
				<span class="submitButton"><INPUT type="button" value="ajax提交" onClick="javascript:ajaxSubmit();"></span>
				 -->
				<input type="checkbox" name="agreebbrule"  checked="checked" /> <label for="agreebbrule">同意<a href="agreement.htm" target="_blank">网站服务条款</a></label>
				<span id="token_error" class="error">${error['token']}</span>  
			</div>
		</div>
	</div>
	<div class="push"><!-- 把div#footer推下去 --></div>
</form>
</div>
<#--引入页脚-->
<@include action="${newPublic_3}"/>

</body></html>